﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using AutoMapper;
using GMS.IServices;
using GMS.IServices.Jwt;
using GMS.Models.Dto;
using GMS.Models.Entity;
using GMS.Models.ResponseDto;
using GMS.Models.SearchDto;
using GMS.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GMS.API.Controllers
{
    [Authorize]
    [ApiController]
    [Route("MenuInfo")]
    public class MenuInfoController : ControllerBase
    {
        private readonly IMenuInfoService _menuInfoService;
        private readonly IMapper _mapper;
        private readonly IJwtService _jwtService;

        public MenuInfoController(IMenuInfoService menuInfoService, IMapper mapper, IJwtService jwtService)
        {
            _menuInfoService = menuInfoService;
            _mapper = mapper;
            _jwtService = jwtService;
        }

        /// <summary>
        /// 获取菜单层级数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("TierAll")]
        public async Task<IActionResult> TierAllAsync()
        {
            var responseDtos = await _menuInfoService.GetTierAllAsync();
            return StatusCode(200, new { code = 200, message = "获取菜单层级数据成功", data = responseDtos });
        }

        [HttpGet("Tier")]
        public async Task<IActionResult> TierAsync()
        {
            var responseDtos = await _menuInfoService.GetTierAsync();
            return StatusCode(200, new { code = 200, message = "获取菜单数据成功", data = responseDtos });
        }

        [HttpGet("All")]
        public async Task<IActionResult> AllAsync([FromQuery] MenuInfoSearchDto dto)
        {
            var entities = await _menuInfoService.GetAllAsync(dto);
            return StatusCode(200, new { code = 200, message = "获取全部菜单成功", data = entities });
        }

        [HttpGet("Load")]
        public async Task<IActionResult> LoadAsync(Guid parentId)
        {
            var entities = await _menuInfoService.GetMenusAsync(parentId);
            var responseDtos = _mapper.Map<List<MenuInfoResponseDto>>(entities);
            return StatusCode(200, new { code = 200, message = "Load菜单数据成功", data = responseDtos });
        }

        [HttpGet("Level")]
        public async Task<IActionResult> LevelAsync()
        {
            var entities = await _menuInfoService.GetLevelAsync();
            var responseDtos = _mapper.Map<List<MenuInfoResponseDto>>(entities);
            return StatusCode(200, new { code = 200, message = "初始化一级菜单成功", data = responseDtos });
        }

        [HttpGet("Top")]
        public async Task<IActionResult> GetTop()
        {
            var entity = await _menuInfoService.GetTopLevel();
            var responseDto = _mapper.Map<MenuInfoResponseDto>(entity);
            return StatusCode(200, new { code = 200, message = "获取顶层菜单成功", data = responseDto });
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("Added")]
        public async Task<IActionResult> AddAsync(MenuInfoDto dto)
        {
            var res = await _menuInfoService.AddAsync(dto);
            return res > 0
                ? StatusCode(200, new { code = 200, message = "添加菜单成功" })
                : StatusCode(500, new { code = 500, message = "添加菜单失败" });
        }

        [HttpPut("Edited")]
        public async Task<IActionResult> Updated(MenuInfoDto dto)
        {
            var entity = _mapper.Map<MenuInfo>(dto);
            var res = await _menuInfoService.UpdatedAsync(entity);
            return res > 0
                ? StatusCode(200, new { code = 200, message = "修改菜单成功" })
                : StatusCode(500, new { code = 500, message = "修改菜单失败" });
        }

        [HttpDelete("Deleted")]
        public async Task<IActionResult> Delete(Guid id)
        {
            var res = await _menuInfoService.DeleteAsync(id);
            return res > 0 ?
                StatusCode(200, new { code = 200, message = "删除菜单成功" })
                : BadRequest(new { code = 500, message = "删除菜单失败!" });
        }

        [HttpGet("UserMenus")]
        public async Task<IActionResult> GetUserMenus()
        {
            //TODO:这里根据Token来获取
            var claims = (ClaimsIdentity)HttpContext.User.Identity;
            var payload = await _jwtService.ParseClaim(claims);
            var responseDtos = await _menuInfoService.GetTierMenusByUserAsync(payload.Id);
            return StatusCode(200, new { code = 200, message = "获取用户菜单权限成功", data = responseDtos });
        }
    }
}